﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Warensoft.Unity.Communication.Client.DataClient
{
	public class DataPropertySet:ICollection<DataProperty>
	{
        private Dictionary<string, DataProperty> properties=new Dictionary<string,DataProperty> ();
        public void Add(DataProperty item)
        {
            this.properties.Add(item.ColumnName,item);
        }

        public void Clear()
        {
            this.properties.Clear();
        }

        public bool Contains(DataProperty item)
        {
            return this.properties.ContainsValue(item);
        }

        public void CopyTo(DataProperty[] array, int arrayIndex)
        {
            array = this.properties.Values.Skip (arrayIndex).ToArray ();
        }

        public int Count
        {
            get { return this.properties .Count ; }
        }

        public bool IsReadOnly
        {
            get {return false ; }
        }

        public bool Remove(DataProperty item)
        {
            return this.properties.Remove(item.ColumnName);
        }

        public IEnumerator<DataProperty> GetEnumerator()
        {
            return this.properties.Values.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.properties.AsEnumerable().GetEnumerator ();
        }
        public DataProperty this[string columnName]
        {
            get
            {
                return this.properties[columnName];
            }
        }
        public DataProperty this[int index]
        {
            get
            {
                return this.properties.ToArray()[index].Value;
            }
        }
        public static DataPropertySet Create(byte[]buffer)
        {
            DataPropertySet set = new DataPropertySet();
            using (MemoryStream ms=new MemoryStream (buffer))
            {
                using (BinaryReader br=new BinaryReader (ms))
                {
                    int count = br.ReadInt32();
                    for (int i = 0; i < count ; i++)
                    {
                        int length = br.ReadInt32();
                        byte[] propertyBuffer = new byte[length];
                        br.Read(propertyBuffer, 0, length);
                        var property = DataProperty.Create(propertyBuffer);
                        set.Add(property);
                    }
                    
                }
            }

            return set;
        }
        public byte[] ToArray()
        {
            byte[] buffer = null;

            using (MemoryStream ms=new MemoryStream ())
            {
                using (BinaryWriter bw=new BinaryWriter (ms))
                {
                    bw.Write(this.properties .Count );
                    foreach (var property in this.properties)
                    {
                        var propertyBuffer = property.Value.ToArray();
                        bw.Write(propertyBuffer.Length);
                        bw.Write(propertyBuffer,0,propertyBuffer .Length);
                    }
                }
                buffer = ms.ToArray();
            }
            return buffer;
        }
    }
}
